from itertools import count
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

# 创建图形和坐标轴
fig, ax = plt.subplots()
x = np.linspace(0, 6*np.pi, 200)
line, = ax.plot(x, np.sin(x))
index = count() # 创建一个计数器, 0,1,2,3...

# 动画更新函数
def update(frame):
    offset = next(index)
    line.set_ydata(np.sin(x + offset/(2*np.pi)))  # 更新数据
    return line,

# 创建动画
ani = FuncAnimation(fig, update, frames=100, interval=100, blit=True)
"""
frames 帧数
interval 每帧间隔时间
blit 是否只更新变化的部分以提高性能
"""
plt.show()